<?php
require_once 'simplegraph.class.php';
require_once 'constants.inc.php';

class FieldPredicateMap extends SimpleGraph {
  var $uri;

  function FieldPredicateMap($uri) {
    $this->uri = $uri;
  }

  function set_label($label) {
    $this->add_literal_triple( $this->uri, RDFS_LABEL, $label);
  }

  function get_label() {
    return $this->get_first_literal($this->uri, RDFS_LABEL);
  }

  function set_comment($value) {
    $this->add_literal_triple( $this->uri, RDFS_COMMENT, $value);
  }

  function get_comment() {
    return $this->get_first_literal($this->uri, RDFS_COMMENT);
  }

  function add_mapping($p, $name) {
    $mapping_uri = $this->uri . '#' . $name;
    $this->add_resource_triple( $this->uri, FRM_MAPPEDDATATYPEPROPERTY, $mapping_uri);
    $this->add_resource_triple( $mapping_uri, FRM_PROPERTY, $p);
    $this->add_literal_triple( $mapping_uri, FRM_NAME, $name);
    return $mapping_uri;
  }

  function remove_mapping($p, $name) {
    $index = $this->get_index();
    foreach ($index[$this->uri][FRM_MAPPEDDATATYPEPROPERTY] as $mapping) {
      if (($mapping['type'] == 'iri' || $mapping['type'] == 'bnode') && isset($index[$mapping['val']]) ) {
        $candidate_mapping_uri = $mapping['val'];
        foreach ( $index[$candidate_mapping_uri][FRM_PROPERTY] as $mapped_property_info) {
          if ( ($mapped_property_info['type'] == 'iri' || $mapped_property_info['type'] == 'bnode') && $mapped_property_info['val'] == $p) {
            foreach ( $index[$candidate_mapping_uri][FRM_NAME] as $mapped_name_info) {
              if ( ($mapped_name_info['type'] != 'iri' && $mapped_name_info['type'] != 'bnode') && $mapped_name_info['val'] == $name) {
                $this->remove_resource_triple( $this->uri, FRM_MAPPEDDATATYPEPROPERTY, $candidate_mapping_uri);
                $this->remove_triples_about($candidate_mapping_uri);
              }
            }
          }
        }
      }
    }
  }


}
?>